/*
 * pps_bom
 */
package com.pitop.assembly.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.assembly.dao.AssemblyImageDAO;
import com.pitop.assembly.entity.AssemblyImage;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.AssemblyImageException;
import com.pitop.core.exceptions.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("assemblyImageSV")
@Slf4j
public class AssemblyImageSVImpl extends BaseSVImpl<AssemblyImage, Long> implements AssemblyImageSV {

    @Autowired
    private AssemblyImageDAO assemblyImageDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return assemblyImageDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(AssemblyImage entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象AssemblyImage
     * @param id 
     * @param assemblyCode 组件编码
     * @param code 图纸编码
     * @return AssemblyImage
     */
    @Override
    public AssemblyImage load(Long id,String assemblyCode,String code) {
       if(id==null&&assemblyCode==null&&code==null){
             throw new AssemblyImageException(BaseException.BaseExceptionEnum.Illegal_Param);
       }

       Map<String,Object> param=new HashMap<>();
       param.put("id",id);
       param.put("assemblyCode",assemblyCode);
       param.put("code",code);
       return assemblyImageDAO.load(param);
    }


    /**
     * 加载一个对象AssemblyImage 通过id
     * @param id 
     * @return AssemblyImage
     */
    @Override
    public AssemblyImage loadById(Long id) {
       if(id==null){
           throw new AssemblyImageException(BaseException.BaseExceptionEnum.Illegal_Param);
       }
       return assemblyImageDAO.loadById(id);
    }
    /**
     * 加载一个对象AssemblyImage 通过assemblyCode
     * @param assemblyCode 组件编码
     * @return AssemblyImage
     */
    @Override
    public AssemblyImage loadByAssemblyCode(String assemblyCode) {
       if(assemblyCode==null){
           throw new AssemblyImageException(BaseException.BaseExceptionEnum.Illegal_Param);
       }
       return assemblyImageDAO.loadByAssemblyCode(assemblyCode);
    }
    /**
     * 加载一个对象AssemblyImage 通过code
     * @param code 图纸编码
     * @return AssemblyImage
     */
    @Override
    public AssemblyImage loadByCode(String code) {
       if(code==null){
           throw new AssemblyImageException(BaseException.BaseExceptionEnum.Illegal_Param);
       }
       return assemblyImageDAO.loadByCode(code);
    }



    /**
     * 删除对象AssemblyImage
     * @param id * @param assemblyCode 组件编码* @param code 图纸编码
     * @return AssemblyImage
     */
    @Override
    public void delete(Long id,String assemblyCode,String code) {
       if(id==null&&assemblyCode==null&&code==null){
           throw new AssemblyImageException(BaseException.BaseExceptionEnum.Illegal_Param);
       }
       Map<String,Object> param=new HashMap<>();
       param.put("id",id);
       param.put("assemblyCode",assemblyCode);
       param.put("code",code);
       assemblyImageDAO.delete(param);
    }



    /**
    * 查询AssemblyImage分页
    *
    * @param assemblyImage  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<AssemblyImage>
    */
    @Override
    public List<AssemblyImage> list(AssemblyImage assemblyImage, int offset, int limit) {
        if (offset < 0) {
           offset = 0;
        }

        if (limit < 0) {
           limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (assemblyImage != null) {
           map = JSON.parseObject(JSON.toJSONString(assemblyImage, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
        return assemblyImageDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(AssemblyImage assemblyImage){
        Map<String, Object> map = null;
        if (assemblyImage != null) {
           map = JSON.parseObject(JSON.toJSONString(assemblyImage, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return assemblyImageDAO.count(map);
    }

    /**
     * 查询AssemblyImage分页
     *
     * @param id  * @param assemblyCode  组件编码* @param code  图纸编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<AssemblyImage>
     */
    @Override
    public List<AssemblyImage> list(Long id,String assemblyCode,String code,int offset, int limit) {
        if (offset < 0) {
          offset = 0;
        }

        if (limit < 0) {
          limit = Page.limit;
        }
        Map<String,Object> param=new HashMap<>();
        param.put("id",id);
        param.put("assemblyCode",assemblyCode);
        param.put("code",code);
        return assemblyImageDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id,String assemblyCode,String code) {
        Map<String,Object> param=new HashMap<>();
        param.put("id",id);
        param.put("assemblyCode",assemblyCode);
        param.put("code",code);
        return assemblyImageDAO.count(param);
    }

}
